﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using FlyHorse.LIMS.Model;
using FlyHorse.LIMS.WinApp.LanguageResource;
using System.Data.Entity;

namespace FlyHorse.LIMS.WinApp.BaseManageForms.StandardSemiForms
{
    public partial class StandardSemiMainForm : DevExpress.XtraEditors.XtraForm
    {
        BLL.StandardSemiBll standardSemiBll = new BLL.StandardSemiBll();
        BLL.TestItemBll testItemBll = new BLL.TestItemBll();
        BLL.GoodBll goodsBll = new BLL.GoodBll();
        BLL.ToolBll toolBll = new BLL.ToolBll();
        OJCB4Entities db = new OJCB4Entities();
        public StandardSemiMainForm()
        {
            InitializeComponent();
        }

        private void StandardSemiMainForm_Load(object sender, EventArgs e)
        {
            //grid的绑定取样位置
            var sSamplingPlaceList = toolBll.GetSamplingPlaceList();
            repositoryItemLookUpEdit1.DisplayMember = "SamplingPlaceName";
            repositoryItemLookUpEdit1.ValueMember = "SamplingPlaceID";
            repositoryItemLookUpEdit1.DataSource = sSamplingPlaceList;

            var testItemList = testItemBll.LoadEntitiesAsNoTracking(m => true).ToList();
            repositoryItemLookUpEdit3.DataSource = testItemList;
            repositoryItemLookUpEdit3.DisplayMember = "ChineseName";
            repositoryItemLookUpEdit3.ValueMember = "TestItemID";

            var goodsList = goodsBll.LoadEntitiesAsNoTracking(m => true).ToList();
            repositoryItemLookUpEdit2.DataSource = goodsList;
            repositoryItemLookUpEdit2.DisplayMember = "GoodsName";
            repositoryItemLookUpEdit2.ValueMember = "GoodsID";

            InitGrid();
        }


        private void InitGrid()
        {
            var models = db.StandardSemis.Where(m => true).OrderBy(m => m.Good.GoodsName).ThenBy(m => m.SamplingPlaceID).ThenBy(m => m.FormOrder).ToList();
            this.gridControl1.DataSource = models;
        }

        private void ButtonItem_add_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            StandardSemiDetail standardSemiDetail = new StandardSemiDetail(InitGrid);
            standardSemiDetail.ShowDialog();
        }

        private void ButtonItem_edit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //ShowEditWindow();
        }

        private void ShowEditWindow()
        {
            string id = Convert.ToString(this.gridView1.GetFocusedRowCellValue("StandardSemiID"));
            StandardSemi model = standardSemiBll.LoadEntities(m => m.StandardSemiID == id).FirstOrDefault();
            if (model != null)
            {
                StandardSemiDetail detailForm = new StandardSemiDetail(model);
                detailForm.ShowDialog();
            }

        }

        private void ButtonItem_Delete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            int selectCount = gridView1.SelectedRowsCount;
            if (selectCount == 0)
            {
                XtraMessageBox.Show(ResourceOverall.SelectNull);
                return;
            }
            int[] selectRows = gridView1.GetSelectedRows();

            DialogResult result = XtraMessageBox.Show(ResourceOverall.ConfirmDelete, ResourceOverall.SystemWarningStr, MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            if (result == DialogResult.OK)
            {
                object sourse = this.gridControl1.DataSource;
                List<StandardSemi> dataSourse = (List<StandardSemi>)sourse;
                List<StandardSemi> delList = new List<StandardSemi>();
                for (int i = 0; i < selectRows.Length; i++)
                {
                    int row = selectRows[i];
                    int dataRowIndex = this.gridView1.GetDataSourceRowIndex(row);
                    StandardSemi standard = dataSourse[dataRowIndex];
                    delList.Add(standard);
                }
                int resCount = BulkDeleteSemiStandard(delList);
                if (resCount > 0)
                {
                    InitGrid();
                    XtraMessageBox.Show(ResourceOverall.DeleteSucceed);
                }
            }
        }



        private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {

            int row = e.RowHandle;
            int dataRowIndex = this.gridView1.GetDataSourceRowIndex(row);
            object sourse = this.gridControl1.DataSource;
            List<StandardSemi> dataSourse = (List<StandardSemi>)sourse;
            StandardSemi standard = dataSourse[dataRowIndex];
            // StandardSemi standard = db.StandardSemis.Find(standardTemp.StandardSemiID);
            if (!String.IsNullOrEmpty(standard.MinValue))
            {
                try
                {
                    Convert.ToDouble(standard.MinValue);
                }
                catch (Exception ex)
                {
                    standard.MinValue = null;
                    XtraMessageBox.Show(ex.Message);
                    return;
                }
            }

            if (!String.IsNullOrEmpty(standard.MaxValue))
            {
                try
                {
                    Convert.ToDouble(standard.MaxValue);
                }
                catch (Exception ex)
                {
                    standard.MaxValue = null;
                    XtraMessageBox.Show(ex.Message);
                    return;
                }
            }

            if (!String.IsNullOrEmpty(standard.MaxValue) && !String.IsNullOrEmpty(standard.MinValue))
            {
                try
                {
                    if (Convert.ToDouble(standard.MinValue) > Convert.ToDouble(standard.MaxValue))
                    {
                        XtraMessageBox.Show(ResourceOverall.ValueCompareErr);
                        this.gridView1.SetRowCellValue(row, e.Column, null);
                        return;
                    }
                }
                catch (Exception ex)
                {
                    XtraMessageBox.Show(ex.Message);
                    return;
                }

            }
            db.Entry<StandardSemi>(standard).State = EntityState.Modified;
            db.SaveChanges();
        }

        private void StandardSemiMainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.gridView1.CloseEditor();
        }

        private void ButtonItem_Order_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FlyHorse.LIMS.WinApp.OtherForms.SetOrderForm form = new FlyHorse.LIMS.WinApp.OtherForms.SetOrderForm(ChangeOrder);
            form.ShowDialog();
        }

        private void ChangeOrder(TestItem testItem, int order)
        {
            int count = ChangeOrderBll(testItem, order);
            XtraMessageBox.Show(ResourceOverall.EditSucceed);
            InitGrid();
        }


        /// <summary>
        /// 批量删除过程标准
        /// </summary>
        /// <param name="standardList"></param>
        /// <returns></returns>
        public int BulkDeleteSemiStandard(List<StandardSemi> standardList)
        {
            foreach (var standard in standardList)
            {
                StandardSemi standardSemi = db.StandardSemis.Find(standard.StandardSemiID);
                db.Set<StandardSemi>().Remove(standardSemi);
            }
            return db.SaveChanges();
        }


        /// <summary>
        /// 批量修改过程报告的顺序
        /// </summary>
        /// <param name="testItem"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        public int ChangeOrderBll(TestItem testItem, int order)
        {
            var standardSemiList = db.StandardSemis.Where(m => m.TestItemID == testItem.TestItemID);
            foreach (var standardSemi in standardSemiList)
            {
                standardSemi.FormOrder = order;
            }
            return db.SaveChanges();
        }



    }
}